package com.fanghaoran.notes.service.impl;

import com.alibaba.fastjson.JSON;
import com.fanghaoran.notes.bean.MyResult;
import com.fanghaoran.notes.bean.OrderDetail;
import com.fanghaoran.notes.bean.RefundDetail;
import com.fanghaoran.notes.bean.SellDetail;
import com.fanghaoran.notes.dao.OrderDao;
import com.fanghaoran.notes.dao.ProductDao;
import com.fanghaoran.notes.dao.impl.OrderDaoImpl;
import com.fanghaoran.notes.dao.impl.ProductDaoImpl;
import com.fanghaoran.notes.entity.Item;
import com.fanghaoran.notes.entity.Orders;
import com.fanghaoran.notes.entity.Refund;
import com.fanghaoran.notes.service.OrderService;
import com.fanghaoran.notes.util.Constants;
import com.fanghaoran.notes.util.JdbcUtil;
import com.fanghaoran.notes.util.StringUtil;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

/**
 * @Author: win11
 * @Date: 2023/04/23/9:56
 * @Description: order 服务层实现类
 */
public class OrderServiceImpl implements OrderService {

    OrderDao orderDao=new OrderDaoImpl();
    ProductDao productDao=new ProductDaoImpl();

    @Override
    public MyResult addOrder(Orders order) throws SQLException {
        Integer rows = orderDao.insertOrder(order);
        return StringUtil.returnOfUpdate(rows);
    }

    @Override
    public MyResult addItem(Item item) throws SQLException {
        Integer rows = orderDao.insertItem(item);
        return StringUtil.returnOfUpdate(rows);
    }

    @Override
    public MyResult showOrder(Integer uId) throws Exception {
        List<Orders> orders = orderDao.selectOrderByUid(uId);
        return StringUtil.returnOfSelect(orders);
    }

    @Override
    public MyResult showOrderDetail(String oId) throws Exception {
        List<OrderDetail> orderDetails = orderDao.selectOrderDetail(oId);
        return StringUtil.returnOfSelect(orderDetails);
    }

    @Override
    public MyResult modifyState(String oId,int oState) throws Exception {

        /*//修改订单状态
        orderDao.updateState(oId,oState);
        System.out.println("异常测试");
        //修改商品库存和销量
        //获取订单中各产品对应的信息
        List<OrderDetail> orderDetails = orderDao.selectOrderDetail(oId);
        for (OrderDetail orderDetail : orderDetails) {
            //获取商品id
            int pid = orderDetail.getPid();
            //获取商品原本的销量和库存
            int sales = orderDetail.getSales();
            int stock = orderDetail.getStock();
            //获取购买量
            int num = orderDetail.getNum();
            //购买后的销量和库存
            sales+=num;
            stock-=num;
            //修改商品信息
            productDao.updateProductAfterBuy(pid,sales,stock);
        }
        System.out.println("修改后");

        return new MyResult(Constants.SUCCESS,null);*/

        Connection connection=null;
        try {
            connection= JdbcUtil.getConnection();
            //开启事务
            connection.setAutoCommit(false);

            //修改订单状态
            orderDao.updateState(oId,oState);
            System.out.println("你执行了吗");
            //修改商品库存和销量
            //获取订单中各产品对应的信息
            List<OrderDetail> orderDetails = orderDao.selectOrderDetail(oId);
            for (OrderDetail orderDetail : orderDetails) {
                //获取商品id
                int pid = orderDetail.getPid();
                //获取商品原本的销量和库存
                int sales = orderDetail.getSales();
                int stock = orderDetail.getStock();
                //获取购买量
                int num = orderDetail.getNum();
                //购买后的销量和库存
                sales+=num;
                stock-=num;
                //修改商品信息
                productDao.updateProductAfterBuy(pid,sales,stock);
            }

            //提交事务
            connection.commit();
            return new MyResult(Constants.SUCCESS,null);
        }catch (SQLException e) {
            //回滚事务
            if (connection != null) {
                System.out.println("抓到了");
                connection.rollback();
            }
            return new MyResult(Constants.FAIL, null);
        } finally {
            //释放连接资源
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    @Override
    public MyResult showSellDetail(Integer uId) throws Exception {
        List<SellDetail> sellDetails = orderDao.selectSellByUid(uId);
        return StringUtil.returnOfSelect(sellDetails);
    }

    @Override
    public MyResult showRefund(int iid) throws Exception {
        List<Refund> refunds = orderDao.selectRefundByIid(iid);
        return StringUtil.returnOfSelect(refunds);
    }

    @Override
    public MyResult addRefund(int iid, String reason, int state, int uId) throws SQLException {
        Integer rows = orderDao.insertRefund(iid, reason, state, uId);
        return StringUtil.returnOfUpdate(rows);
    }

    @Override
    public MyResult showRefundForSeller(int uId) throws Exception {
        List<RefundDetail> refundDetails = orderDao.selectRefundByUid(uId);
        return StringUtil.returnOfSelect(refundDetails);
    }

    @Override
    public MyResult modifyRefundState(int iid, int state) throws SQLException {
        Integer rows = orderDao.updateRefundState(iid, state);
        return StringUtil.returnOfUpdate(rows);
    }
}
